///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2007-2009 ILOG, S.A.
//  All Rights Reserved.
//  The following is ILOG Source Code.  No modifications may be made to the  
//  ILOG Source Code.  Any usage of the ILOG Source Code is subject to 
//  the terms and conditions of the ILOG End User License Agreement   
//  applicable to this ILOG software product.
//
///////////////////////////////////////////////////////////////////////////////

package ilog.gauges.gaugesClasses
{
  import mx.skins.ProgrammaticSkin;
  import mx.styles.CSSStyleDeclaration;
  import mx.styles.StyleManager;
  import mx.events.FlexEvent;
  import mx.core.IDataRenderer;
  
  /**
   * A base class for tick renderers that supports taking style from 
   * a CSS selector (<code>CSSStyleDeclaration</code>) in addition to 
   * <code>IStyleClient</code>.
   *   
   * @see #styleName
   * @since ILOG Elixir 2.0
   * @ilog.reviewed SH 5 May 08 (only the class description, the rest is a copy/paste of already proofread text; SH: modifs anyway!)
   */
  public class TickRendererBase extends ProgrammaticSkin implements IDataRenderer
  {
    /**
     * Constructor.
     */
    public function TickRendererBase()
    {
    }
    
    private var _data:Object;
    
    [Inspectable(environment="none")]
    [Bindable("dataChange")]       
    
    /**
     * The <code>TickItem</code> that corresponds to this tick.
     * This value is assigned by the owning scale. You can use the value
     * to customize the drawing of the tick according to <code>TickItem</code>.
     * 
     * @see ilog.gauges.TickItem
     */    
    public function get data():Object
    {
      return _data;
    }
    
    /**
     * @private
     */
    public function set data(value:Object):void
    {
      _data = value;
      invalidateDisplayList();
      dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
    }
    
    private var _styleName:String;
    private var _cssSelector:CSSStyleDeclaration;
    
    /**
     * The <code>styleName</code> this renderer refers to (either an <code>IStyleClient</code>
     * or a string representing a CSS selector).
     */
    public override function get styleName():Object
    {
      return (_styleName != null) ? _styleName : super.styleName;
    }
        
    /**
     *  @private
     */
    public override function set styleName(value:Object):void
    {
      if (value is String && value != _styleName) {
        _cssSelector = StyleManager.getStyleDeclaration("."+value);
        if (_cssSelector) { // we might not have a selector => fallback 
          _styleName = value as String;                         
          invalidateDisplayList();
          return;
        }         
      }
      super.styleName = value;
    }  
    
    /**
     * @private
     */
    public override function getStyle(styleProp:String):*
    {
      return (_styleName != null) ? _cssSelector.getStyle(styleProp) : super.getStyle(styleProp);
    }          
  }
}
